package leetcode;

public class ReconstructNumFromEnglish {

	private final String[] words = {"zero", "one", "two", "there", "four", "five", "six", "seven", "eight",
			"nine"};
	
	private final char[] del = {'z', 'w', 'u', 'f', 'x', 'v', 'g', 'i', 't', 'e'};
	
	private final int[] order = {0, 2, 4, 5, 6, 7, 8, 9, 3, 1};
	
	public static void main(String[] args) {
		ReconstructNumFromEnglish object = new ReconstructNumFromEnglish();
		String s = "fviefuro";
		System.out.println(object.originalDigits(s));
	}
	
    public String originalDigits(String s) {
        if(s == null || s.length() <= 0){
        	return s;
        }
        int length = s.length();
        int[] map = new int[26];
        for(int i = 0; i < length; i++){
        	map[s.charAt(i) - 'a']++;
        }
        StringBuilder sBuilder = new StringBuilder();
        
        //记录每个数字出现的次数
        int[] count = new int[10];
        
        for(int i = 0; i < del.length; i++){
        	if(map[del[i] - 'a'] != 0){
        		count[order[i]] = map[del[i] - 'a'];
        	}
        	int wordLength = words[order[i]].length();
        	for(int j = 0; j < wordLength; j++){
        		map[words[order[i]].charAt(j) - 'a'] -= count[order[i]];
        	}
        }
        for(int i = 0; i < count.length; i++){
        	for(int k = 0; k < count[i]; k++){
        		sBuilder.append(i);
        	}
        }
        return sBuilder.toString();
    }
}
